import React, { Component } from 'react';
import { notification } from 'antd';
import { connect } from 'react-redux';
import LoginForm from '../../components/loginForm';
import '../../assets/sass/login.scss';
import { login } from '../../action/actionLogin';
import loginLogo from '../../assets/image/login_logo.png';

class Login extends Component {
    componentDidUpdate () {
        const { loginData, history } = this.props;
        if (loginData.code === 0) {
            this.noteTip('success', '登录成功！');
            history.push('/admin/orderm');
        } else if (loginData.code === 100) {
            this.noteTip('error', loginData.data.message);
        }
    }
    noteTip (type, content) {
        notification[type]({
            message: '登录提示',
            description: content,
            duration: 2,
        });
    }
    render () {
        const { dispatch, isFetching } = this.props;
        return <div className='login'>
            <div className='login-form'>
                <img className='login-logo' src={loginLogo}/>
                <LoginForm isFetching={isFetching} onSubmitHandle={data => {
                    dispatch(login(data));
                }}/>
            </div>
        </div>;
    }
}
function select (state) {
    return {
        loginData: state.loginData,
        isFetching: state.requestStatus.status,
    };
}
export default connect(select)(Login);
